package ar.com.photo_admin.domain

class ShoppingCart extends DomainObject {
	
	User user
	
	static hasMany = [items : ShoppingCartItem]
	
	static BelongsTo = [user : User]
	
    static constraints = {
    }
	
	List getSelectedPhotos(){
		def photos = items?.collect { it.photo }
		return photos != null ? photos : [] ;
	}
	
	static mapping = {
		items cascade: "all-delete-orphan"
	}
	
	def removePhoto(EventPhoto photo){
		def l = []
		l += this.items
		
		l.each { item ->
			if (item.photo == photo)
				this.removeFromItems(item)
		}
	}
	
	def getPhotographers() {
		items?.collect { it.photo.event.photographer } as Set
	}
	
	def getItemsByPhotographer(Photographer photographer) {
		items?.findAll { ShoppingCartItem item -> item.photo.event.photographer ==  photographer }
	}
	
	def getPhotosByPhotographer(Photographer photographer) {
		def itemsByPhotographer = this.getItemsByPhotographer(photographer)
		itemsByPhotographer.collect { ShoppingCartItem item -> item.photo }
	}
}
